import sys

import requests

from api.base import Login


class merProductList(Login):
    # 查询
    def product_list(self, keywords=None, type=1, **kwargs):
        """查询 1，2，3，4，5，6，7，8"""
        data = {
            "url": '/api/admin/merchant/product/list',
            "method": "get",
            "headers": {'authori-zation': self._mer_token},
            "params": {
                'page': '1',
                'limit': '20',
                'cateId': '',
                'keywords': keywords,
                'type': type,
                'temp': '1726035192',
            }
        }
        return self.mer_send_request(sys._getframe().f_code.co_name, data)

    # 新增商品
    def product_save(self, name):
        """添加商品"""
        data = {
            "url": '/api/admin/merchant/product/save',
            "method": "POST",
            "headers": {'content-type': 'application/json',
                        'authori-zation': self._mer_token},
            "json": {
                'image': 'https://crmebjavamer.oss-cn-beijing.aliyuncs.com/crmebimage/public/store/2024/08/09/329e096ada794a27993d7c1a9b3e6dfe1izzcb4g22.png',
                'sliderImages': [
                    'https://crmebjavamer.oss-cn-beijing.aliyuncs.com/crmebimage/public/store/2024/08/09/329e096ada794a27993d7c1a9b3e6dfe1izzcb4g22.png',
                ],
                'sliderImage': '["https://crmebjavamer.oss-cn-beijing.aliyuncs.com/crmebimage/public/store/2024/08/09/329e096ada794a27993d7c1a9b3e6dfe1izzcb4g22.png"]',
                'name': name,
                'intro': '121212',
                'keyword': '',
                'cateIds': [
                    875,
                ],
                'cateId': '875',
                'unitName': '12',
                'sort': 1,
                'isShow': False,
                'tempId': 285,
                'attrValue': [
                    {
                        'image': 'https://crmebjavamer.oss-cn-beijing.aliyuncs.com/crmebimage/public/store/2024/08/09/329e096ada794a27993d7c1a9b3e6dfe1izzcb4g22.png',
                        'price': 100,
                        'vipPrice': 0.01,
                        'cost': 80,
                        'otPrice': 100,
                        'stock': 1000,
                        'barCode': '',
                        'weight': 1,
                        'volume': 1,
                        'brokerage': 0,
                        'brokerageTwo': 0,
                        'cdkeyId': None,
                        'cdkeyLibraryName': '',
                        'expand': '',
                        'attrValue': '{"规格":"默认"}',
                        'productId': 0,
                    },
                ],
                'attr': [
                    {
                        'attrName': '规格',
                        'attrValues': '默认',
                        'id': 0,
                    },
                ],
                'selectRule': '',
                'isSub': False,
                'isPaidMember': False,
                'content': '',
                'specType': False,
                'couponIds': [],
                'coupons': [],
                'categoryId': 211,
                'guaranteeIds': '',
                'guaranteeIdsList': [],
                'brandId': 0,
                'type': 0,
                'isAutoSubmitAudit': False,
                'isAutoUp': False,
            }
        }
        return self.mer_send_request(sys._getframe().f_code.co_name, data)

    # 上架商品
    def product_up(self, product_id):
        """商品上架"""
        data = {
            "url": '/api/admin/merchant/product/up/{}'.format(product_id),
            "method": "post",
            "headers": {'authori-zation': self._mer_token},
        }
        return self.mer_send_request(sys._getframe().f_code.co_name, data)

    # 下架商品
    def product_down(self, product_id):
        """商品下架"""
        data = {
            "url": f'/api/admin/merchant/product/down/{product_id}',
            "method": "post",
            "headers": {'authori-zation': self._mer_token},
        }
        return self.mer_send_request(sys._getframe().f_code.co_name, data)

    # 删除/放入回收站 delete / recycle
    def product_delete(self, pid, type='recycle'):
        data = {
            "url": '/api/admin/merchant/product/delete',
            "method": "post",
            "headers": {'authori-zation': self._mer_token},
            'json': {
                'id': pid,
                'type': type,
            }
        }
        return self.mer_send_request(sys._getframe().f_code.co_name, data)

    # 批量放入回收站
    def product_batch_recycle(self, id_list: list[int]):
        """批量添加至回收站"""
        data = {
            "url": '/api/admin/merchant/product/batch/recycle',
            "method": "post",
            "headers": {'authori-zation': self._mer_token},
            "json": {"idList": id_list}
        }
        return self.mer_send_request(sys._getframe().f_code.co_name, data)

    # 批量删除
    def product_batch_delete(self, id_list: list[int]):
        """批量删除"""
        data = {
            "url": '/api/admin/merchant/product/batch/delete',
            "method": "post",
            "headers": {'authori-zation': self._mer_token},
            "json": {"idList": id_list}
        }
        return self.mer_send_request(sys._getframe().f_code.co_name, data)


if __name__ == '__main__':
    print(merProductList().product_list())
